//
//  CustomSegmentControl.m
//  SwipeTableView
//
//  Created by Roy lee on 16/5/28.
//  Copyright © 2016年 Roy lee. All rights reserved.
//

#import "CustomSegmentControl.h"
#import "UIView+STFrame.h"
#define RGBColor(r,g,b)     [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]

@interface CustomSegmentControl ()


@property (nonatomic, strong) NSArray * items;
@property (nonatomic, strong) UIView *line;

@end

@implementation CustomSegmentControl
static id _instance;
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}
+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}
- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}
- (id)mutableCopyWithZone:(NSZone *)zone {
    return _instance;
}
//+(CustomSegmentControl *)sharedInstance {
//    static id sharedInstance = nil;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        sharedInstance = [[self alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//    });
//    return sharedInstance;
//}
//- (instancetype)initWithFrame:(CGRect)frame {
//    self = [super initWithFrame:frame];
//    if (self) {
//        [self commonInit];
//    }
//    return self;
//}

- (instancetype)initWithItems:(NSArray *)items {
    self = [self initWithFrame:CGRectZero];
    if (self) {
        [self commonInit];
        if (items.count > 0) {
            self.items = items;
        }
    }
    return self;
}

- (void)commonInit {
    _contentView = [UIView new];
    _contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_contentView];
    _font = [UIFont systemFontOfSize:15];
    _textColor = RGBColor(50, 50, 50);
    _selectedTextColor = RGBColor(0, 0, 0);
    _selectionIndicatorColor = RGBColor(150, 150, 150);
    _items = @[@"Segment0",@"Segment1"];
    _selectedSegmentIndex = 0;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    for (UIView *subView in _contentView.subviews) {
        [subView removeFromSuperview];
    }
    
    _contentView.backgroundColor = _backgroundColor;
    _contentView.frame = self.bounds;
    for (int i = 0; i < _items.count; i ++) {
        UIButton * itemBt = [UIButton buttonWithType:UIButtonTypeCustom];
        itemBt.tag = 666 + i;
        [itemBt setTitleColor:_textColor forState:UIControlStateNormal];
        [itemBt setTitleColor:_selectedTextColor forState:UIControlStateSelected];
        [itemBt setTitle:_items[i] forState:UIControlStateNormal];
        [itemBt.titleLabel setFont:_font];
        CGFloat itemWidth = self.st_width/_items.count;
        itemBt.st_size = CGSizeMake(itemWidth, self.st_height);
        itemBt.st_x    = itemWidth * i;
        if (i == 0) {
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, itemBt.frame.origin.y+itemBt.frame.size.height-3, itemBt.frame.size.width, 3)];
            self.line = line;
            line.backgroundColor = RGBA(16, 142, 233, 1);
            [_contentView addSubview:line];
        }
        
        if (i == 1) {
            UIView *readView = [[UIView alloc]initWithFrame:CGRectMake(itemBt.frame.size.width/2 + 20, 10, 10, 10)];
            self.redView = readView;
            readView.backgroundColor = [UIColor redColor];
            readView.layer.masksToBounds = YES;
            if (self.isRed) {
                self.redView.hidden = NO;
            }else {
                self.redView.hidden = YES;
            }
            readView.layer.cornerRadius = readView.frame.size.width/2;
            [itemBt addSubview:readView];
        }
        
        if (i == _selectedSegmentIndex) {
//            itemBt.backgroundColor = _selectionIndicatorColor;
            itemBt.selected = YES;
        }else {
//            itemBt.backgroundColor = [UIColor clearColor];
        }
        [itemBt addTarget:self action:@selector(didSelectedSegment:) forControlEvents:UIControlEventTouchUpInside];
        [_contentView addSubview:itemBt];
    }
}

- (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex {
    UIButton * oldItemBt      = [_contentView viewWithTag:666 + _selectedSegmentIndex];
    oldItemBt.backgroundColor = [UIColor clearColor];
    oldItemBt.selected        = NO;
    
    UIButton * itemBt      = [_contentView viewWithTag:666 + selectedSegmentIndex];
//    itemBt.backgroundColor = _selectionIndicatorColor;
    self.line.frame = CGRectMake(itemBt.frame.size.width*selectedSegmentIndex, itemBt.frame.origin.y+itemBt.frame.size.height - 3, itemBt.frame.size.width, 3);
    
    itemBt.selected        = YES;
    _selectedSegmentIndex  = selectedSegmentIndex;
}

- (void)didSelectedSegment:(UIButton *)itemBt {
    UIButton * oldItemBt      = [_contentView viewWithTag:666 + _selectedSegmentIndex];
    oldItemBt.backgroundColor = [UIColor clearColor];
    oldItemBt.selected        = NO;
    
//    itemBt.backgroundColor = _selectionIndicatorColor;
    self.line.frame = CGRectMake(itemBt.frame.size.width*(itemBt.tag - 666), itemBt.frame.origin.y+itemBt.frame.size.height - 3, itemBt.frame.size.width, 3);
    itemBt.selected        = YES;
    _selectedSegmentIndex  = itemBt.tag - 666;
    if (self.IndexChangeBlock) {
        self.IndexChangeBlock(_selectedSegmentIndex);
    }
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}

@end





